﻿Imports Sunts.HyperV.Common
Imports HyperVBackup
Imports System.Threading


Public Class frmBackupTest
    Private serviceStarted As Boolean = False
    Private bw As BackupWorker
    Private MainThread As Thread
    Private ServiceLog As String = "servicelog.log"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            serviceStarted = True
            MainThread = New Thread(New ThreadStart(AddressOf MainWorker))
            MainThread.Start()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        serviceStarted = False
        Try
            If bw IsNot Nothing Then
                bw.CancelJob()
            End If
        Catch ex As Exception
            WriteToLogFile("Service stop" & ControlChars.CrLf & ex.Message & ControlChars.CrLf & ex.StackTrace, ServiceLog)
        End Try

    End Sub
    Private Sub MainWorker()
        Dim DatabaseCheck As DatabaseCheck

        While serviceStarted = True
            Try
                DatabaseCheck = New DatabaseCheck(My.Settings.ConnectionString)
                If Not DatabaseCheck.Exists Then
                    WriteToLogFile("The application will close as the database (" & DatabaseCheck.Location & ") could not be found ", ServiceLog)
                    Exit Try
                End If

                bw = New BackupWorker
                bw.Run()
                bw = Nothing

            Catch ex As Exception
                WriteToLogFile(ex.Message & ControlChars.CrLf & ex.StackTrace, ServiceLog)
            Finally
                If (serviceStarted) = True Then
                    Thread.Sleep(New TimeSpan(0, 0, 30))
                End If
            End Try

        End While
    End Sub




End Class